//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------
#include "LinearGradientBrush.h"

LinearGradientBrush::LinearGradientBrush(ID2D1RenderTarget* renderTarget) {
	_renderTarget = renderTarget;

	_linearGradientBrush = 0;

	InitializeBrush();
}

LinearGradientBrush::~LinearGradientBrush(void) {
	
}

void LinearGradientBrush::SetCanvasGradient(CanvasGradient canvasGradient) {
	_canvasGradient = canvasGradient;

	RefreshBrush();
}

ID2D1LinearGradientBrush* LinearGradientBrush::GetGradientBrush() {
	return _linearGradientBrush;
}

void LinearGradientBrush::RefreshBrush() {
	if(_canvasGradient._gradientStops.size() < 0) return;

	D2D1_GRADIENT_STOP* gradientStops = new D2D1_GRADIENT_STOP[_canvasGradient._gradientStops.size()];
	for(int i = 0; i < _canvasGradient._gradientStops.size(); i++) {
		gradientStops[i] = _canvasGradient._gradientStops[i];
	}

	ID2D1GradientStopCollection* gradientStopCollection;

	if(_linearGradientBrush != 0) {
		_linearGradientBrush->Release();
		_linearGradientBrush = 0;
	}

	HRESULT hr = _renderTarget->CreateGradientStopCollection(
		gradientStops,
		_canvasGradient._gradientStops.size(),
		D2D1_GAMMA_2_2,
		D2D1_EXTEND_MODE_CLAMP,
		&gradientStopCollection
	);

	if(FAILED(hr)) {
		//MessageBox(NULL, "createD2DBrushes::CreateSolidColorBrush failed", "Direct2DContext", 0);
	}

	hr = _renderTarget->CreateLinearGradientBrush(
		D2D1::LinearGradientBrushProperties(
		D2D1::Point2F(_canvasGradient._x0, _canvasGradient._y0),
		D2D1::Point2F(_canvasGradient._x1, _canvasGradient._y1)),
		gradientStopCollection,
		&_linearGradientBrush
	);

	if(FAILED(hr)) {
		//MessageBox(NULL, "createD2DBrushes::CreateSolidColorBrush failed", "Direct2DContext", 0);
	}
}

/*
void LinearGradientBrush::Clear() {
	if(_linearGradientBrush != 0) {
		_linearGradientBrush->Release();
		_linearGradientBrush = 0;
	}
}
*/

void LinearGradientBrush::InitializeBrush() {
	
}